<%@ page import="com.mysql.jdbc.Driver"%>
<%@ page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> 
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*,java.util.Random" %>
<!DOCTYPE html>
<html>
<head>
<title>找回密码
</title>

</head>
<body>
<%
String userName="root";
String userpasswd="";
String dbName="a0713225307";

    request.setCharacterEncoding("UTF-8");
	String mail=request.getParameter("email");//用户Email
	String newpass=request.getParameter("newpass");//生成的用户的新密码
	// System.out.println(mail);
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	String u="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userpasswd+"&useUnicode=true&characterEncoding=UTF-8";
    java.sql.Connection con=java.sql.DriverManager.getConnection(u,"root","");
	String sql="select uName from users where email='"+mail+"'";
	Statement statement=con.createStatement();
	ResultSet rs=statement.executeQuery(sql);



if(rs.next()){

String smtphost = "smtp.qq.com"; // 发送邮件服务器
String user = "418206775@qq.com"; // 邮件服务器登录用户名
String password = "419959@@fan"; // 邮件服务器登录密码
String from = "418206775@qq.com"; // 发送人邮件地址
String to = "1491935994@qq.com"; // 接受人邮件地址
String subject = "找回密码————Rita的小窝"; // 邮件标题
String body = "<h3>亲爱的用户<b style='color:red'>"+rs.getString(1)+"</b>：</h3><br>您好!<p>感谢您注册我们的网站，您的新密码是：<br><i style='color:red;font-size:28px'>"+newpass+"</i><br><br>为了您的账户安全，请登录后尽快修改您的密码。</p>"; // 邮件内容


// 以下为发送程序，用户无需改动
try {
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth","true");
Session ssn = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(ssn);

InternetAddress fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);

message.setSubject(subject);
message.setContent(body,"text/html;charset=utf-8");

Transport transport = ssn.getTransport("smtp");
transport.connect(smtphost, user, password);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
//transport.send(message);
transport.close();
%>
<jsp:forward page="user/changePasswd.jsp">
<jsp:param name="uname" value="<%=rs.getString(1)%>"/>
<jsp:param name="newpass" value="<%=newpass%>" />
</jsp:forward>
<%
} catch(Exception m) {
out.println(m.toString());
m.printStackTrace();
}
}else{
	response.sendRedirect("forget.jsp?flag=2");
}

rs.close();
statement.close();
con.close();
%>
</body>
</html>